package org.jsola.hr.service.impl;

import org.jsola.common.StrKit;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ITransferAccountsBankBatchDAO;
import org.jsola.hr.dto.TransferAccountsBankBatchAddDTO;
import org.jsola.hr.dto.TransferAccountsBankBatchUpdateDTO;
import org.jsola.hr.entity.TransferAccountsBankBatchDO;
import org.jsola.hr.query.TransferAccountsBankBatchQuery;
import org.jsola.hr.service.ITransferAccountsBankBatchService;
import org.jsola.hr.vo.TransferAccountsBankBatchListVO;
import org.jsola.hr.vo.TransferAccountsBankBatchVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 转账批次(平安银行)
 *
 * @author zhr
 */
@Service("hrTransferAccountsBankBatchServiceImpl")
public class TransferAccountsBankBatchServiceImpl implements ITransferAccountsBankBatchService {

    @Autowired
    private ITransferAccountsBankBatchDAO transferAccountsBankBatchDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public TransferAccountsBankBatchVO save(TransferAccountsBankBatchAddDTO transferAccountsBankBatchAddDTO, TokenUser tokenUser) {
        // 转DO
        TransferAccountsBankBatchDO transferAccountsBankBatchDO = transferAccountsBankBatchAddDTO.to(TransferAccountsBankBatchDO.class);
        // 保存
        transferAccountsBankBatchDO = save(transferAccountsBankBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return transferAccountsBankBatchDO.to(TransferAccountsBankBatchVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(TransferAccountsBankBatchUpdateDTO transferAccountsBankBatchUpdateDTO, TokenUser tokenUser) {
        //转DO
        TransferAccountsBankBatchDO transferAccountsBankBatchDO = transferAccountsBankBatchUpdateDTO.to(TransferAccountsBankBatchDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(transferAccountsBankBatchDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... transferAccountsBankBatchIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) transferAccountsBankBatchIds);
    }


    @Override
    public TransferAccountsBankBatchVO selectById(String transferAccountsBankBatchId, String siteId) {
        TransferAccountsBankBatchDO transferAccountsBankBatchDO = selectDOById(transferAccountsBankBatchId, siteId);
        if (transferAccountsBankBatchDO == null) {
            return null;
        }
        return transferAccountsBankBatchDO.to(TransferAccountsBankBatchVO.class);
    }

    @Override
    public List<TransferAccountsBankBatchListVO> select(TransferAccountsBankBatchQuery transferAccountsBankBatchQuery, String siteId) {
        List<TransferAccountsBankBatchDO> transferAccountsBankBatchDOList = selectDO(transferAccountsBankBatchQuery, siteId);
        if(CollectionUtils.isEmpty(transferAccountsBankBatchDOList)) {
            return transferAccountsBankBatchDOList == null ? null : new ArrayList<>();
        }
        return transferAccountsBankBatchDOList.stream()
                .map(transferAccountsBankBatchDO -> transferAccountsBankBatchDO.to(TransferAccountsBankBatchListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(TransferAccountsBankBatchQuery transferAccountsBankBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsBankBatchQuery,siteId);
        return transferAccountsBankBatchDAO.selectCountByExample(example);
    }

    @Override
    public Page<TransferAccountsBankBatchListVO> selectPage(TransferAccountsBankBatchQuery transferAccountsBankBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsBankBatchQuery,siteId);
        Page<TransferAccountsBankBatchDO> page = transferAccountsBankBatchDAO.selectPageByExample(example,
                transferAccountsBankBatchQuery.getPageNo(),
                transferAccountsBankBatchQuery.getPageSize());

        return page.to(TransferAccountsBankBatchListVO.class);
    }

    @Override
    public TransferAccountsBankBatchDO selectDOById(String transferAccountsBankBatchId, String siteId) {
        return listById(transferAccountsBankBatchId, siteId);
    }

    @Override
    public List<TransferAccountsBankBatchDO> selectDO(TransferAccountsBankBatchQuery transferAccountsBankBatchQuery, String siteId) {
        Example example = buildExample(transferAccountsBankBatchQuery,siteId);
        return transferAccountsBankBatchDAO.selectByExample(example);
    }

    @Override
    public TransferAccountsBankBatchDO selectDOByThirdVoucher(String thirdVoucher, String siteId) {
        if (StrKit.isEmpty(thirdVoucher)) {
            return null;
        }
        Example example = new Example(TransferAccountsBankBatchDO.class);
        example.and()
                .andEqualTo("thirdVoucher", thirdVoucher)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        return transferAccountsBankBatchDAO.selectOneByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param transferAccountsBankBatchQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(TransferAccountsBankBatchQuery transferAccountsBankBatchQuery, String siteId) {
        Example example = new Example(TransferAccountsBankBatchDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,transferAccountsBankBatchQuery.getOrders());
        return example;
    }
}




